package com.jingzhe.controller.sys;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jingzhe.biz.sys.domain.SysMenu;
import com.jingzhe.biz.sys.service.ISysMenuService;
import com.jingzhe.controller.CommonWeb;
import com.jingzhe.controller.sys.vo.MenuEditReqVo;
import com.jingzhe.core.utils.ErrorMsgUtil;
import com.jingzhe.core.web.converter.Result;

@Controller
@RequestMapping("/sys/menu")
public class MenuWeb extends CommonWeb {
    private final static Logger  logger = LoggerFactory.getLogger(MenuWeb.class);
    
    //菜单列表页
    private final static String MENU_LIST_PAGE = "sys/menu/menuList";
    
    @Resource
    private ISysMenuService sysMenuService;
    
    /**
     * 跳转到菜单列表页
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String toMenu(HttpServletRequest request) {
//        Map<String, SysMenu> sysMenuMap = sysMenuService.selectAllByParentId(0, new LinkedHashMap<String, SysMenu>());
//        request.setAttribute("sysMenuMap", sysMenuMap);
        return MENU_LIST_PAGE;
    }
    
    /**
     * 根据上级菜单，获取下次菜单列表
     * @param parentId
     * @return
     */
    @RequestMapping(value = "/select-by-parent-id/{parentId}", method = RequestMethod.GET)
    @ResponseBody
    public List<SysMenu> selectByParentId(@PathVariable Integer parentId) {
        return sysMenuService.selectByParentId(parentId);
    }
    
    /**
     * 查找菜单详情
     * @param menuId
     * @return
     */
    @RequestMapping(value = "/select-by-id/{menuId}", method = RequestMethod.GET)
    @ResponseBody
    public SysMenu selectById(@PathVariable Integer menuId) {
        return sysMenuService.selectById(menuId);
    }
    
    /**
     * 保存菜单-新增和修改
     * @param menuEditReqVo
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/save-menu", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> saveMenu(@Valid MenuEditReqVo menuEditReqVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ErrorMsgUtil.invalidResult(bindingResult);
        }
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(menuEditReqVo, sysMenu);
        sysMenu.setUpdateBy(getUserId());
        if (menuEditReqVo.getId() == null) {
        	sysMenu.setCreateBy(getUserId());
        	sysMenuService.insertMenu(sysMenu);
        }else {
        	sysMenuService.updateMenu(sysMenu);
        }
        return new Result<String>("保存成功");
    }
    
    /**
     * 根据菜单id删除菜单
     * @param menuId
     * @return
     */
    @RequestMapping(value = "/delete-by-id/{menuId}", method = RequestMethod.DELETE)
    @ResponseBody
    public Result<String> deleteById(@PathVariable Integer menuId){
    	sysMenuService.deleteMenu(menuId, getUserId());
    	return new Result<String>("删除成功");
    }
}
